<?php
$isReloadable = true;
require_once "../../../pers/inc.config.php";
require_once OVF_FS_WEB_ROOT.'/app/_inc/inc.session.php';
require_once OVF_FS_WEB_ROOT.'/app/_inc/funct.html.php';
require_once OVF_FS_WEB_ROOT.'/app/_inc/class.ovfForm.php';
require_once OVF_FS_WEB_ROOT.'/app/_inc/class.ovfLista.php';

$objSess->setReqSV('rcom', 'idAnag', 0);
$idAnag = intval($objSess->getSV('rcom', 'idAnag'));

$oForm = new ovfForm('anagraf');
//dbg_($oForm);
$sSql = "SELECT ".$oForm->getCampiSelect()." from ovf_anagrafiche A WHERE id_anag = ".$idAnag;
$objDB->query($sSql);
if (!$oForm->aCampiDB = $objDB->fetch()) {
	backOnErr("id. errato '".reqVar('idAnag')."'");
}
$aNomiAge = array();
$sSql = "SELECT au.id_utente, u.cognome, u.nome FROM ovf_anag_utenti au "
   ." LEFT JOIN ovf_utenti u ON u.id_utente = au.id_utente"
   ." WHERE au.id_anag = ".reqVarInt('idAnag');
$objDB->query($sSql,'selAge');
while($acX = $objDB->fetch('selAge')) {
   $aNomiAge[] = $acX['nome']." ".$acX['cognome'];
}
$oForm->aCampiDB['CALC_nomi_agenti'] = implode(", ",$aNomiAge);

$oForm->db2form();

$oLista = new ovfLista('referent','default');

$aWhere = array(" s.id_anag = ".$idAnag);
$sOrder = '';
$sX = '(';
foreach($oLista->aCampiList as $kX => $aX) {
   if (substr($aX['campo'],0,2)!='S_') continue;
   if ($sOrder == '') {
      $sOrder = " ORDER BY s.".substr($aX['campo'],2);
   }
   $sX .= "s.".substr($aX['campo'],2)." LIKE '%".addslashes(reqVar('pagifilt'))."%' OR ";
}
if (reqVar('pagifilt')!='' && $sX != '(') {
   $aWhere[] = substr($sX,0,-4).')';
}
$sWhere = (count($aWhere)==0)? "":" WHERE ".implode(" AND ",$aWhere);
$sSql = "SELECT count(*) AS num_rec " .
      " FROM ovf_anag_referent s " .$sWhere;
//dbg_($sSql);
$objDB->query($sSql,'selAsezId');
$acRec = $objDB->fetch('selAsezId');
$numRec = $acRec['num_rec'];
$link = $_SERVER['PHP_SELF']."?".$sXSNQS;
$startOffset = 0;
$endOffset = 0;
$sFiltro = ovfGetFiltro($link);
$sPaginaz = ovfGetPaginaz($link, $numRec, $startOffset, $endOffset);

$sSql = "SELECT s.id_referent " .
      " FROM ovf_anag_referent s " .$sWhere.$sOrder.
      " LIMIT ".$startOffset.",".($endOffset-$startOffset);
//dbg_($sSql);
$objDB->query($sSql,'selAsezId');

$sCmdLP = "<a class=\"cmd1\" href=\"anagPdf.php?idAnag=".reqVar('idAnag')."&$sXSNQS\" target=\"_blank\">"
   ."<img src='".OVF_URL_ROOT."/app/_img/ico_pdf.gif' border='0'> pdf</a>&nbsp;&nbsp;&nbsp;\n";
$sCmdLX = "<a class=\"cmd1\" href=\"../cont/contList.php?idAnag=".reqVar('idAnag')."&$sXSNQS\" >contatti</a>&nbsp;&nbsp;&nbsp;\n";
$sCmdLX .= "&nbsp;&nbsp;&nbsp;<a class=\"cmd1\" href=\"../docu/docuList.php?idAnag=".reqVar('idAnag')."&$sXSNQS\" >documenti</a>&nbsp;&nbsp;&nbsp;\n";

$sCmdRA = "&nbsp;&nbsp;&nbsp;";
$sCmdRA .= "&nbsp;&nbsp;&nbsp;<a class=\"cmd1\" href=\"../../agen/impeIns.php?listIdAge=".$objSess->idUtente
   ."&anagX=".urlencode($oForm->aCampiDB['A_intestaz1']." (".$oForm->aCampiDB['A_id_anag'].")")
   ."&$sXSNQS\" >fissa impegno</a>\n";
$sCmdRA .= "&nbsp;&nbsp;&nbsp;<a class=\"cmd1\" href=\"../cont/contIns.php?idAnag=".$oForm->aCampiDB['A_id_anag']
   ."&$sXSNQS\" >registra contatto</a>\n";
$sCmdRA .= "&nbsp;&nbsp;&nbsp;<a class=\"cmd1\" href=\"anagMod.php?idAnag=".reqVar('idAnag')."&$sXSNQS\" >modifica scheda</a>\n";

$sCmdRT = cmdTorna();

$sCmdRR = "&nbsp;&nbsp;&nbsp;";
$sCmdRR .= "<a href='".OVF_URL_ROOT."/app/rcom/refe/refeIns.php?$sXSNQS' class='cmd1'>"._t('nuovo referente')."</a>";
testaHtml('Anagrafe','Scheda',$msgPag,'rcom');
?>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
	<?=getHtmlRigaRif();?>
   <tr>
      <td class="cmd1" align="left" colspan='10'>
			<? echo  $sCmdLX;?>
      </td>
      <td class="cmd1" align="right" colspan='10'>
			<? echo  $sCmdRA.$sCmdRT;?>
      </td>
   </tr>
   <tr>
      <td class="tit1" colspan='20' align='center'>Anagrafica</td>
   </tr>
<?
echo $oForm->getHtmlCampiView();
?>
   <tr>
      <td class="cmd1" align="left" colspan='5'>
         <? echo  $sCmdLP;?>
      </td>
      <td class="cmd1" align="right" colspan='15'>
         <? echo  $sCmdRR.$sCmdRA.$sCmdRT;?>
      </td>
   </tr>
   <tr>
      <td class="cmd1" align="left" colspan='10'>
         <?=$sFiltro?>
      </td>
      <td class="cmd1" align="right" colspan='10'>
         <?=$sPaginaz?>
      </td>
   </tr>
   <tr>
      <td class="tit1" align="center" colspan='20'>Referenti</td>
   </tr>
   <tr>
      <?php echo $oLista->getHtmlLab();?>
   </tr>
<? 
$clRF = 'txt2';
while($acZ = $objDB->fetch('selAsezId')) { 
   $clRF = ($clRF=='txt1')?'txt2':'txt1'; 
   $sSql = "SELECT ".$oLista->getCampiSelect('S')
      ." FROM ovf_anag_referent S "
      ." WHERE S.id_referent = ".$acZ["id_referent"];
   $acX = $objDB->query($sSql,'selAsezIdX');
   $acX = $objDB->fetch('selAsezIdX');
   $aOpt1 = array("class"=>$clRF);
   $aOpt1['href'] = OVF_URL_ROOT."/app/rcom/refe/refeView.php?id=".$acZ["id_referent"]."&$sXSNQS";
   echo "<tr>\n";
   echo $oLista->getHtmlDat($acX,$aOpt1);
   echo "</tr>\n";
 } ?>
   <tr>
      <td class="cmd1" align="right" colspan='20'>
         <?=$sPaginaz?>
      </td>
   </tr>
   <tr>
      <td class="cmd1" align="left" colspan='10'>
         <?=$sCmdLP?>
      </td>
      <td class="cmd1" align="right" colspan='10'>
         <?=$sCmdRR.$sCmdRT?>
      </td>
   </tr>
</table>
<?
codaHtml();
exit();
